package com.auction.system.action.auction;


import com.auction.system.base.BaseResponse;
import com.auction.system.domain.dto.auction.OrderEvaluate;
import com.auction.system.domain.query.auction.OrderEvaluateQuery;
import com.auction.system.enums.EnumReturnResult;
import com.auction.system.service.auction.IOrderEvaluateService;
import com.auction.system.service.auction.IOrderInfoService;
import com.auction.system.utils.StringUtil;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * @ClassName OrderEvaluateAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/orderEvaluate")
public class OrderEvaluateAction {

    @Autowired
    private IOrderEvaluateService orderEvaluateService;

    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 进入所有订单评价页面
     * @return
     */
    @RequestMapping(value = "/allList",method = RequestMethod.GET)
    public String allList(){
        return "orderEvaluate/allList";
    }

    /**
     * 进入我的订单评价页面
     * @return
     */
    @RequestMapping(value = "/myList",method = RequestMethod.GET)
    public ModelAndView myList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("evaluateUserId", UserUtil.getUserId());
        mav.setViewName("orderEvaluate/myList");
        return mav;
    }

    /**
     * 获取订单评价数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public OrderEvaluateQuery getList(OrderEvaluateQuery query) {
        try {
            query = orderEvaluateService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取订单评价列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取订单评价数据异常");
        }
        return query;
    }

    /**
     * 获取订单评价数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMyList",method = RequestMethod.POST)
    public OrderEvaluateQuery getMyList(OrderEvaluateQuery query) {
        try {
            query.setEvaluateUserId(Long.parseLong(UserUtil.getUserId()));
            query = orderEvaluateService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取订单评价列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取订单评价数据异常");
        }
        return query;
    }

    /**
     * 进入订单评价页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView preAdd(Long id,String evaluateDir){
        ModelAndView mav = new ModelAndView();
        mav.addObject("orderId", id);
        mav.addObject("evaluateDir", evaluateDir);
        mav.setViewName("orderEvaluate/add");
        return mav;
    }

    /**
     * 订单评价
     * @param orderEvaluate
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated OrderEvaluate orderEvaluate, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("OrderEvaluate-add参数有误");
                log.info("OrderEvaluate-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            //判断关联的订单是否存在
            if(StringUtil.isNull(orderEvaluate.getOrderId()) || StringUtil.isNull(orderInfoService.get(orderEvaluate.getOrderId()))){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("关联的订单不存在");
                return response;
            }
            orderEvaluate.setEvaluateUserId(Long.parseLong(UserUtil.getUserId()));
            orderEvaluateService.add(orderEvaluate);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("订单评价成功");
        }catch (Exception e){
            log.error("订单评价异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("订单评价失败");
        }
        return response;
    }

}
